/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ConnectDB;

import Validate_Connect.DataInteraction;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.sql.*;
import net.sf.jasperreports.view.JasperViewer;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;

/**
 *
 * @author Shinichi
 */
public class ConnectDB {

//    public static Connection ConnectDatabase() {
//        Properties _pro = new Properties();
//        File file = new File("configs.properties");
//        try {
//            if(!file.exists()){
//                file.createNewFile();
//                Config _config = new Config();
//                _pro.setProperty("serverName", "localhost");
//                _pro.setProperty("databaseName", "AITSM");
//                _pro.setProperty("port", "1433");
//                _pro.setProperty("userName", "sa");
//                _pro.setProperty("password", "123456");
//                FileOutputStream fos = new FileOutputStream(file);
//                _pro.store(fos, "");
//                fos.close();
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        
//        InputStream _is = null;
//        try {
//            _is = new FileInputStream(file);
//        } catch (FileNotFoundException ex) {
//            Logger.getLogger(ConnectDB.class.getName()).log(Level.SEVERE, null, ex);
//        }
//        try {
//            _pro.load(_is);
//        } catch (IOException ex) {
//            Logger.getLogger(ConnectDB.class.getName()).log(Level.SEVERE, null, ex);
//        }
//
//        String _drivername = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
//        String _username = _pro.getProperty("userName");
//        String _passwrod = _pro.getProperty("password");
//        String _port = _pro.getProperty("port");
//        String _databasename = _pro.getProperty("databaseName");
//        String _serverName = _pro.getProperty("serverName");
//        String _url = "jdbc:sqlserver://" + _serverName + ":" + _port;
//
//        Connection cn = null;
//        try {
//            Class.forName(_drivername);
//            cn = DriverManager.getConnection(_url + ";databasename=" + _databasename, _username, _passwrod);
//        } catch (SQLException ex) {
//            Logger.getLogger(ConnectDB.class.getName()).log(Level.SEVERE, null, ex);
//        } catch (ClassNotFoundException ex) {
//            Logger.getLogger(ConnectDB.class.getName()).log(Level.SEVERE, null, ex);
//        }
//        return cn;
//    }
    
    public static void runReport(String reportFile) {
      try{
         //JasperDesign jasperDesign = JRXmlLoader.load(reportFile);
         //JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
         Connection cn = DataInteraction.getConnect();
         JasperPrint jasperPrint = JasperFillManager.fillReport(reportFile, null, cn);
         JasperViewer.viewReport(jasperPrint,false);
      }catch(Exception ex) {
//         String connectMsg = "Could not create the report " + ex.getMessage() + " " + ex.getLocalizedMessage();
//         System.out.println(connectMsg);
          System.err.println(ex.getMessage());
          ex.printStackTrace();
      }
   }
    
//    public static void main(String[] args) {
//        Connection cn = ConnectDatabase();
//        String fileReport = "src\\report1.jasper";
//        runReport(fileReport); 
//    }
}
